package com.hmkj.web.controller.entertain;

import com.hmkj.common.entity.ResultEntity;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.constant.PointNid;
import com.hmkj.core.model.operate.PointRuleModel;
import com.hmkj.core.po.operate.PointRule;
import com.hmkj.core.po.user.PointLog;
import com.hmkj.core.po.user.User;
import com.hmkj.core.service.amount.PointLogService;
import com.hmkj.core.service.amount.RechargeService;
import com.hmkj.core.service.operate.PointRuleService;
import com.hmkj.core.service.user.UserService;
import com.hmkj.web.controller.GenericController;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "积分任务相关")
@Slf4j
@RestController
@RequestMapping(GenericController.BASE_API_URL + "/point")
public class PointController extends GenericController<Integer, PointRule>{
    @Resource
    private PointRuleService pointRuleService;
    @Resource
    private PointLogService pointLogService;
    @Resource
    private RechargeService rechargeService;
    @Resource
    private UserService userService;

    @ApiOperation(value = "积分任务", notes = "积分任务")
    @ResponseBody
    @RequestMapping(value = "getPointTaskList", method = RequestMethod.POST)
    public ResultEntity getPointTaskList() {
        Map<String,Object> data = new HashMap<>();
        User u =  getLoginUser();
        boolean b = false;
        if(null != u){
            b = true;
        }
        List<PointRule> rule = pointRuleService.select(new PointRule(pr->{
            pr.setStatus(PointRule.STATUS.T1.code);
            pr.setDelFlag(PointRule.DELFLAG.NORMAL.code);
        }));
        Integer point = 0;
        if(b){
            point =  userService.selectByPrimaryKey(u.getId()).getPoint();
        }
        //模拟交易奖励积分次数、实盘交易奖励积分次数
        Integer rewardCountSimu = redisService.get(ConfigNID.REWARD_COUNT_SIMU,Integer.class);
        Integer rewardCount = redisService.get(ConfigNID.REWARD_COUNT,Integer.class);
        List<PointRuleModel> ruleModel =  new ArrayList<>();
        if(CollectionUtil.isNotEmpty(rule)){
            for(PointRule r : rule){
                PointRuleModel model = new PointRuleModel();
                BeanUtils.copyProperties(r,model);
                if(model.getNid().equals(PointNid.T4.nid)){
                    String[] args = model.getValue().split(";");
                    int i = 0;
                    for(String arg : args){
                        i = i+1;
                    }
                    model.setRewardCount(i);
                }else if(model.getNid().equals(PointNid.T5.nid)){
                    model.setRewardCount(rewardCountSimu);
                }else if(model.getNid().equals(PointNid.T6.nid)){
                    model.setRewardCount(rewardCount);
                }else {
                    model.setRewardCount(1);
                }
                if(b){
                    //查询用户完成任务程度
                    if(model.getNid().equals(PointNid.T1.nid)){
                        model.setFinishCount(1);
                    }else if(model.getNid().equals(PointNid.T2.nid)){
                        List<PointLog> signLogs =  pointLogService.findSignPointByTime(u.getId());
                        if(CollectionUtil.isNotEmpty(signLogs)){
                            model.setFinishCount(1);
                        }else {
                            model.setFinishCount(0);
                        }
                    }else if(model.getNid().equals(PointNid.T3.nid)){
                        List<PointLog> rechargeLogs = pointLogService.select(new PointLog(p->{
                            p.setUserId(u.getId());
                            p.setNid(PointNid.T3.nid);
                            p.setDelFlag(PointLog.DELFLAG.NORMAL.code);
                        }));
                        if (CollectionUtil.isNotEmpty(rechargeLogs)){
                            model.setFinishCount(1);
                        }else {
                            model.setFinishCount(0);
                        }
                    }else if(model.getNid().equals(PointNid.T4.nid)){
                        BigDecimal rechargeMoney =  rechargeService.findRechargeAllMoney(u.getId());
                        String[] args = model.getValue().split(";");
                        int count = 0;
                        for (String arg : args) {
                            if (StrUtil.isNotBlank(arg)) {
                                String[] items = arg.split(",");
                                BigDecimal limit = new BigDecimal(items[0]);
                                if (rechargeMoney.compareTo(limit) >= 0) {
                                    count++;
                                }
                            }
                        }
                        model.setFinishCount(count);
                    }else if(model.getNid().equals(PointNid.T5.nid)){
                        List<PointLog> simLogs = pointLogService.select(new PointLog(pl->{
                            pl.setNid(PointNid.T5.nid);
                            pl.setUserId(u.getId());
                            pl.setDelFlag(PointLog.DELFLAG.NORMAL.code);
                        }));
                        if(simLogs!=null&&simLogs.size()>0){
                            if(simLogs.size()<= rewardCountSimu){
                                model.setFinishCount(simLogs.size());
                            }else {
                                model.setFinishCount(rewardCountSimu);
                            }
                        }else{
                            model.setFinishCount(0);
                        }
                    }else if(model.getNid().equals(PointNid.T6.nid)){
                        List<PointLog> tradeLogs = pointLogService.select(new PointLog(pl->{
                            pl.setNid(PointNid.T6.nid);
                            pl.setUserId(u.getId());
                            pl.setDelFlag(PointLog.DELFLAG.NORMAL.code);
                        }));
                        if(tradeLogs!=null&&tradeLogs.size()>0){
                            if(tradeLogs.size()<= rewardCount){
                                model.setFinishCount(tradeLogs.size());
                            }else {
                                model.setFinishCount(rewardCount);
                            }
                        }else{
                            model.setFinishCount(0);
                        }
                    }
                }else {
                    model.setFinishCount(0);
                }
                ruleModel.add(model);
            }
        }
        data.put("login",b);
        data.put("pointRule",ruleModel);
        data.put("point",point);
        return ok(data);
    }


}
